<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="餐台前缀" prop="prefix">
        <el-input v-model="dataForm.prefix" placeholder="餐台前缀"></el-input>
      </el-form-item>
    <el-form-item label="餐台编号">
      <el-input-number :min="1" :max="10000" v-model="dataForm.number" placeholder="餐台编号"></el-input-number>
      <span style="color: red">注:如果不填写编号，餐台名将以前缀命名</span>
    </el-form-item>

    <el-form-item label="所属区域" prop="regionId">
		<el-select :disabled="dataForm.id !== 0" v-model="dataForm.storeTypeId" placeholder="类型" @change="getRegionByStoreTypeId">
			<el-option
			 v-for="item in storeTypeList"
			 :label="item.storeTypeName"
			 :key="item.id"
			 :value="item.id">
			</el-option>
		</el-select>
		<el-select :disabled="dataForm.id !== 0" v-model="dataForm.regionId" placeholder="区域">
			<el-option
			 v-for="item in regionList"
			 :label="item.regionName"
			 :key="item.id"
			 :value="item.id">
			</el-option>
		</el-select>
    </el-form-item>
    <el-form-item label="座位数" prop="seatNum">
      <el-input-number :min="1" :max="100" v-model="dataForm.seatNum" placeholder="座位数"></el-input-number>
    </el-form-item>
    <el-form-item label="备注">
      <el-input v-model="dataForm.reamrk" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
				disabledSubmit:false,
        visible: false,
        regionList:[],
		    storeTypeList:[],
        dataForm: {
          id: 0,
          diningTableNo: '',
          diningTableName: '',
          sellerId: '',
          regionId: '',
          seatNum: '',
          status: '',
          qrCode: '',
          reamrk: '',
          prefix:'',
          number:'',
		      storeTypeId:''
        },
        dataRule: {
          diningTableNo: [
            { required: true, message: '餐台编号不能为空', trigger: 'blur' }
          ],
          diningTableName: [
            { required: true, message: '餐台名称不能为空', trigger: 'blur' }
          ],
          sellerId: [
            { required: true, message: '所属租户不能为空', trigger: 'blur' }
          ],
          regionId: [
            { required: true, message: '所属区域不能为空', trigger: 'blur' }
          ],
          seatNum: [
            { required: true, message: '座位数不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态(1空闲2就餐)不能为空', trigger: 'blur' }
          ],
          qrCode: [
            { required: true, message: '点餐二维码不能为空', trigger: 'blur' }
          ],
          number: [
            { required: true, message: '序号不能为空', trigger: 'blur' }
          ],
          prefix: [
            { required: true, message: '前缀不能为空', trigger: 'blur' }
          ],
          reamrk: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
	  getRegionByStoreTypeId(){
		  this.$http({
		  	url: this.$http.adornUrl('/store/tstoreregion/list'),
		  	method: 'get',
		  	params: this.$http.adornParams({
		  	  'page': 1,
		  	  'limit': 100,
		  	  'status': 1,
			  'storeTypeId': this.dataForm.storeTypeId
		  	})
		  }).then(({data}) => {
		  	if (data && data.code === 0) {
		  		this.regionList = data.page.list;
		  	}
		  })
	  },

      init (id) {
			//餐厅类型
			this.$http({
				url: this.$http.adornUrl('/store/tstoretype/list'),
				method: 'get',
			}).then(({data}) => {
				if (data && data.code === 0) {
					this.storeTypeList = data.page.list
				} else {
					this.storeTypeList = []
				}
			})

        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstorediningtable/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.diningTableNo = data.tStoreDiningTable.diningTableNo
                this.dataForm.diningTableName = data.tStoreDiningTable.diningTableName
                this.dataForm.sellerId = data.tStoreDiningTable.sellerId
                this.dataForm.regionId = data.tStoreDiningTable.regionId
                this.dataForm.seatNum = data.tStoreDiningTable.seatNum
                this.dataForm.prefix = data.tStoreDiningTable.prefix
                this.dataForm.number = data.tStoreDiningTable.number
                this.dataForm.status = data.tStoreDiningTable.status.toString()
                this.dataForm.qrCode = data.tStoreDiningTable.qrCode
                this.dataForm.reamrk = data.tStoreDiningTable.reamrk
                this.dataForm.storeTypeId = data.tStoreDiningTable.storeTypeId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
				this.disabledSubmit = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstorediningtable/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'diningTableNo': this.dataForm.diningTableNo,
                'diningTableName': this.dataForm.diningTableName,
                'sellerId': this.dataForm.sellerId,
                'regionId': this.dataForm.regionId,
                'seatNum': this.dataForm.seatNum,
                'status': this.dataForm.status,
                'qrCode': this.dataForm.qrCode,
                'reamrk': this.dataForm.reamrk,
                'prefix': this.dataForm.prefix,
                'number': this.dataForm.number,
                'storeTypeId':this.dataForm.storeTypeId,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
										this.disabledSubmit = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.disabledSubmit = false
                this.$message.error(data.msg)
              }
            })
          }else {
            this.disabledSubmit = false
          }
        })
      }
    }
  }
</script>
